package com.shxc.smarttrade.currency.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.just.library.AgentWeb;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.base.BaseActivity;
import com.shxc.smarttrade.utils.Loger;

import org.xchris.view.annotation.ContentView;
import org.xchris.view.annotation.Event;
import org.xchris.view.annotation.ViewInject;

/**
 * web
 */
@ContentView(R.layout.activity_web)
public class WebActivity extends BaseActivity {

    @ViewInject(R.id.linearlayout)
    LinearLayout linearlayout;
    @ViewInject(R.id.title_textview)
    TextView title_textview;
    @ViewInject(R.id.left_imageview)
    ImageView left_imageview;

    private AgentWeb mAgentWeb;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        String url = getIntent().getExtras().getString("url");
        String title = getIntent().getExtras().getString("title");

        Loger.e("title",title);

        title_textview.setText(title);
        left_imageview.setVisibility(View.VISIBLE);

        mAgentWeb = AgentWeb.with(this)//传入Activity
                .setAgentWebParent(linearlayout, new LinearLayout.LayoutParams(-1, -1))//传入AgentWeb 的父控件 ，如果父控件为 RelativeLayout ， 那么第二参数需要传入 RelativeLayout.LayoutParams
                .useDefaultIndicator()// 使用默认进度条
                .setIndicatorColor(Color.parseColor("#38385D"))
                .createAgentWeb()//
                .ready()
                .go(url);

    }

    @Event({R.id.left_imageview})
    private void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_imageview:
                if (mAgentWeb.getWebCreator().get().canGoBack()) {
                    mAgentWeb.back();
                } else {
                    finish();
                }
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (mAgentWeb.handleKeyEvent(keyCode, event)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        mAgentWeb.clearWebCache();
        mAgentWeb.getWebLifeCycle().onDestroy();
        super.onDestroy();

    }
}
